capture program drop estimate_bydecile
program define estimate_bydecile
clear
	use `1', clear 
	collapse (mean) size `2' (median) med_size=size med_`2'=`2', by($product iso2 decile hs hs6_2002) 
	g l`2'=log(`2')

	g fdecile=log(log(11 - decile) - log(10 - decile))
	replace fdecile=log(log(1.1)) if decile==1 // this is for size from pctile 0.01 to pctile 0.1 
	replace fdecile=log(log(10)) if decile==10 // this is for size from pctile 0.9 to pctile 0.99 
	replace l`2'=log(`2' /0.9) if decile==1 |decile==10 // to account for the fact that we drop the top1% and bottom 1%
	egen p=group($product)
	qui tab iso3, g(c_*)
	fese l`2' fdecile c_*, a(hs) s(rs_hs6iso_) oonly
	collapse (mean) rs* `2', by($product hs6)
	label var rs "Relationship stickiness"
	save rs_measure_`3'_`4', replace // Relationship stickiness (rs) measures by product are in this dataset 
end

